package com.example.dessertstoressm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dessertstoressm.Mapper.*;
import com.example.dessertstoressm.Service.*;
import com.example.dessertstoressm.Service.impl.AccountServiceImpl;
import com.example.dessertstoressm.domain.*;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class testConnect {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SignOnMapper signOnMapper;
    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private BannerDataMapper bannerDataMapper;

    @Resource
    private AccountService accountService;
    @Test
    void test1(){
        System.out.println(accountService.getAccountByUsername("j2ee"));

        Account account = accountService.getAccountByUsernameAndPassword("j2ee","j2ee");
//        account.getSignOn().setPassword("654321");
        accountService.updateAccount(account);

        Account account1;
        account1 = account;
        account1.setUsername("acc");
        accountService.insertAccount(account1);
    }


    @Test
    void testAccount(){
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("userid","1234");
        Account account = this.accountMapper.selectOne(wrapper);
        if(account != null){
            QueryWrapper<SignOn> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("username",account.getUsername());
            SignOn signOn = this.signOnMapper.selectOne(wrapper1);
            account.setSignOn(signOn);
            QueryWrapper<Profile> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("userid",account.getUsername());
            Profile profile = this.profileMapper.selectOne(wrapper2);
            account.setProfile(profile);

            QueryWrapper<BannerData> wrapper3 = new QueryWrapper<>();
            wrapper3.eq("favcategory",profile.getFavouriteCategoryId());
            BannerData bannerData = this.bannerDataMapper.selectOne(wrapper3);
            account.setBannerData(bannerData);
            System.out.println(account);
        }else {
            System.out.println("null");
        }
    }

    @Resource
    private SequenceService sequenceService;
    @Test
    void testSequence(){
        Sequence sequence = new Sequence("ordernum",1004);
        System.out.println(sequenceService.getSequence(sequence));

        sequence.setNextId(1006);
        sequenceService.updateSequence(sequence);
    }

    @Resource
    private ProductService productService;
    @Test
    void testProduct(){
        Product product = productService.getProduct("CHEESE-04");
        System.out.println(product);
        List<Product> products = productService.getProductListByCategory("FRUIT");
        System.out.println(products);
        products = productService.searchProductList("布");
        System.out.println(products);
    }

    @Resource
    private LineItemService lineItemService;
    @Test
    void TestLineItem(){
        List<LineItem> lineItems = lineItemService.getLineItemsByOrderId(1002);
        System.out.println(lineItems);

        LineItem lineItem = new LineItem();
        lineItem.setOrderId(1010);
        lineItem.setLineNumber(1);
        lineItem.setItemId("EST-2");
        lineItem.setQuantity(2);
        BigDecimal unitprice = new BigDecimal("23.00");
        lineItem.setUnitPrice(unitprice);
        lineItemService.insertLineItem(lineItem);
    }

    @Resource
    private CategoryService categoryService;
    @Test
    void TestCategory(){
        List<Category> categories = categoryService.getCategoryList();
        System.out.println(categories);

        Category category = categoryService.getCategory("CHOCOLATE");
        System.out.println(category);
    }

    @Resource
    private ItemService itemService;
    @Test
    void TestItem(){
//        Item item = itemService.getItem("EST-10");
//        System.out.println(item);
//
//        System.out.println(itemService.getInventoryQuantity("EST-13"));

//        List<Item> items = itemService.getItemListByProduct("MUSI-01");
//        System.out.println(items);

//        updateInventoryQuantity(Map<String, Object> param)
        Map<String, Integer> param = new HashMap<>();
        param.put("EST-14", 2);
        itemService.updateInventoryQuantity(param);
    }

    @Resource
    private OrderService orderService;
    @Test
    void TestOrder(){
//        List<Order> orders = orderService.getOrdersByUsername("j2ee");
//        System.out.println(orders);
//
//        Order order = orderService.getOrder(1001);
//        System.out.println(order);

        Account account = new Account("j2ee","56789","abc","123","OK",
                "901 San Antonio Road",
                "MS UCUP02-206","Palo","Alto",
                "94303","USA","8996435",null,null,null);
        Cart cart = new Cart();
        Order order1 = new Order();
        order1.initOrder(account,cart);
        System.out.println(order1);
        orderService.insertOrder(order1);
    }

    @Resource
    private CatalogService catalogService;
    @Test
    void TestCatalog(){
        List<Category> categories = catalogService.getCategoryList();
        System.out.println(categories);

        Category category = catalogService.getCategory("FRUIT");
        System.out.println(category);

        Product product = catalogService.getProduct("CHOCO-02");
        System.out.println(product);

        List<Product> products = catalogService.getProductListByCategory("MUSI");
        System.out.println(products);

        List<Product> products1 = catalogService.searchProductList("黑");
        System.out.println(products1);

        List<Item> items = catalogService.getItemListByProduct("MUSI-04");
        System.out.println(items);

        Item item = catalogService.getItem("EST-16");
        System.out.println(item);

        Boolean bool = catalogService.isItemInStock("EST-18");
        System.out.println(bool);
    }

}
